import oprObj from '../config/opr'
import menuData from '../config/menu'
// 角色列表
let roleList = async (ctx, next) => {
  let roleData = await ctx.db.querys(`select * from role`)
  ctx.body = {
    code: 1,
    msg: '用户列表数据获取成功',
    data: {
      roleData,
      oprObj,
      menuData
    }
  }
}


// 角色列表模糊搜索
let searchRoleList = async (ctx, next) => {
  const {
    roleName,
    status
  } = ctx.request.body;
  let roleData = await ctx.db.querys(`select * from role`)

  if (!roleData.length) {
    return ctx.body = {
      code: 0,
      msg: '暂无数据',
      data: []
    }
  }

  if (roleName !== '') {
    roleData = roleData.filter(item => item.roleName.indexOf(roleName) !== -1)

  }

  if (status !== 'all') {
    roleData = roleData.filter(item => item.status.indexOf(status) !== -1)
  }

  ctx.body = {
    code: 1,
    msg: '数据请求成功',
    data: roleData
  }

}


// 冻结账号
let roleIco = async (ctx, next) => {

  let isOpr = await ctx.isJudge(ctx, 'D');

  if (!isOpr) {
    ctx.body = {
      code: 0,
      msg: '您无权操作'
    }
    return
  }
  // 当前登录用户如果存在产出的权限 则可执行冻结
  const {
    roleid
  } = ctx.request.body
  let dbReturn = await ctx.db.querys(`update role set status="freeze" where roleid='${roleid}'`)
  if (!dbReturn.affectedRows) {
    ctx.body = {
      code: 0,
      msg: '操作失败,请重新再来'
    }
    return
  }
  ctx.body = {
    code: 1,
    msg: '冻结成功'
  }
}




// 角色列表黑名单解除冻结
let blackRoleIco = async (ctx, next) => {

  let isOpr = await ctx.isJudge(ctx, 'D');

  if (!isOpr) {
    ctx.body = {
      code: 0,
      msg: '您无权操作'
    }
    return
  }
  // 当前登录用户如果存在产出的权限 则可执行冻结
  const {
    roleid
  } = ctx.request.body
  let dbReturn = await ctx.db.querys(`update role set status="ok" where roleid='${roleid}'`)
  if (!dbReturn.affectedRows) {
    ctx.body = {
      code: 0,
      msg: '操作失败,请重新再来'
    }
    return
  }
  ctx.body = {
    code: 1,
    msg: '解冻成功'
  }
}



// 点击编辑 获取对应数据
let getSetRole = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'E');
  if (!isOpr) {
    ctx.body = {
      code: 0,
      msg: '您无权操作'
    }
    return
  }
  const {
    roleid
  } = ctx.request.body;
  let roleData = await ctx.db.querys(`select * from role where roleid='${roleid}'`)
  ctx.body = {
    code: 1,
    msg: '数据请求成功',
    data: roleData
  }
}


// 编辑之后确定,修改数据库数据
let setRole = async (ctx, next) => {
  const {
    roleName,
    roleid,
    status,
    menulimits,
    oprlimits
  } = ctx.request.body;
  let roleData = await ctx.db.querys(`select * from role where roleid='${roleid}'`)
  if (!roleData.length) {
    return ctx.body = {
      code: 0,
      msg: '未找到该数据',
      data: roleData
    }
  }
  await ctx.db.querys(`update role set roleName='${roleName}',menulimits='[${menulimits}]',roleid='${roleid}',status='${status}',oprlimits='${oprlimits}' where roleid='${roleid}'`)

  ctx.body = {
    code: 1,
    msg: '数据请求成功',
    data: []
  }

}


// 添加账号
let addRole = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'A');

  // 判断当前用户是否可以操作
  if (!isOpr) {
    ctx.body = {
      code: 0,
      msg: '您无权操作'
    }
    return
  }
  // 如果权限够  就可以添加
  const {
    roleName,
    menulimits,
    oprlimits,
    datalimits,
    status
  } = ctx.request.body;
  let roleList = await ctx.db.querys(`select * from role where roleName='${roleName}'`)
  if (roleList.length !== 0) {
    ctx.body = {
      code: 0,
      msg: '已存在该用户名,请更换用户名后重试'
    }
    return
  }
  menulimits.unshift(1)
  let createTime = new Date().getTime()
  let results = await ctx.db.querys(`insert into role (roleName,menulimits,oprlimits,datalimits,status,createTime) values ('${roleName}','[${menulimits}]','${oprlimits}','${datalimits}','${status}','${createTime}')`)


  if (results.affectedRows === 1) {
    ctx.body = {
      code: 1,
      msg: '添加成功'
    }
    return
  }
  ctx.body = {
    code: 0,
    msg: '添加失败'
  }
}


// 通过roleid获取查看权限列表
let getmenulimitsData = async (ctx, next) => {

  const {
    roleid
  } = ctx.request.body
  let menulimitsData = await ctx.db.querys(`select menulimits from role where roleid=${roleid}`)
  let a = menulimitsData[0].menulimits.slice(1, menulimitsData[0].menulimits.length - 1).split(",")
  let arr = []
  a.forEach((item, index) => {
    arr.push(menuData[item])
  })

  let arr1 = []
  arr.forEach(item => {
    if (item.menuName) {
      arr1.push(item.menuName)
    } else {
      arr1.push(item.belongTitle)
    }
  })

  ctx.body = {
    code: 1,
    msg: "查看权限获取成功",
    data: arr1
  }
}


export default {
  roleList,
  searchRoleList,
  roleIco,
  getSetRole,
  setRole,
  addRole,
  blackRoleIco,
  getmenulimitsData
}